package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.*;
import com.example.xpvideo.entity.res.*;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.BargainGoodsMapper;
import com.example.xpvideo.mapper.LsBargainMapper;
import com.example.xpvideo.service.BargainGoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class BargainGoodServiceImpl implements BargainGoodService {
    @Autowired
    private BargainGoodsMapper bargainGoodsMapper;
    @Autowired
    private LsBargainMapper lsBargainMapper;

    @Override
    public ResponseResult<PageResult<List<BargainListRes>>> list(BargainGoodReq bargainGoodReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String name = bargainGoodReq.getName();
        PageHelper.startPage(page, size);

        List<BargainListRes> list = bargainGoodsMapper.BargainGoodList(Wrappers.<LsBargain>query()
                .like(!ObjectUtils.isEmpty(name),"lg.name",name));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<BargainListRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<BargainGoodDetail> detail(Integer bargainId) throws ErrorException {

        BargainGoodDetail bargainGoodDetail= bargainGoodsMapper
                .BargainGoodDetail(Wrappers.<LsBargain>query()
                        .eq("lb.id",bargainId));


        return ResponseResult.responseResult(0, bargainGoodDetail);

    }

    @Override
    public ResponseResult<PageResult<List<BargainRecordRes>>> RecordList(BargainRecordReq bargainRecordReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        Integer id = bargainRecordReq.getId();
        String name = bargainRecordReq.getName();
        Integer status = (Integer) bargainRecordReq.getStatus();
        String sn = bargainRecordReq.getSn();
        String nickname = bargainRecordReq.getNickname();
        String mobile = bargainRecordReq.getMobile();

        PageHelper.startPage(page, size);

        List<BargainRecordRes> list = bargainGoodsMapper.BargainRecord(Wrappers.<LsBargain>query()
                .like(!ObjectUtils.isEmpty(name),"lg.name",name));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<BargainRecordRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> audit(AuditReq auditReq) throws ErrorException{
        Integer id = auditReq.getId();
        Integer status = auditReq.getStatus();
        String remark = auditReq.getRemark();

        LsBargain lsBargain = new LsBargain();
        lsBargain.setId(id);
        lsBargain.setAuditStatus(status);
        lsBargain.setAuditRemark(remark);

        int res = lsBargainMapper.updateLsBargain(lsBargain);

        if(res > 0){
            return ResponseResult.responseResult(0, "审核成功");
        }

        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> reviweAudit(ReviewAuditReq reviewAuditReq) throws ErrorException{
        Integer id = reviewAuditReq.getId();
        String reason = reviewAuditReq.getReason();

        LsBargain lsBargain = new LsBargain();
        lsBargain.setId(id);
        lsBargain.setAuditRemark(reason);
        int res = lsBargainMapper.updateLsBargain(lsBargain);

        if(res > 0){
            return ResponseResult.responseResult(0, "违规重审成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
